<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\supplyDemand\logic;

use plugin\saiadmin\basic\BaseLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper;
use app\supplyDemand\model\supplyDemand;
use plugin\saiadmin\service\OpenSpoutWriter;

/**
 * 供求列表逻辑层
 */
class supplyDemandLogic extends BaseLogic
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new supplyDemand();
    }

    /**
     * 根据用户 ID 和分页限制获取供求信息
     *
     * @param int $userId 用户 ID
     * @param int $limit 分页每页显示的记录数
     * @return \think\Paginator 分页结果集
     */
    public function getSupplyDemand($userId, $limit)
    {
        return $this->model
            ->rightJoin('eb_collection', 'eb_supply_demand.sd_id = eb_collection.collection_id')
            ->field('eb_supply_demand.sd_id,eb_supply_demand.product_name,eb_supply_demand.product_subtitle,eb_supply_demand.image,eb_supply_demand.supply_demand,eb_supply_demand.enterprise_id')
            ->where('eb_collection.user_id', $userId)
            ->where('eb_collection.type', 2)
            // 修正拼写错误，将 ceeate_time 改为 create_time
            ->order('eb_collection.create_time desc')
            ->paginate($limit);
    }
   public function export($where = [])
{
    $query = $this->search($where);
    // ->field('id,name,enterprise_id,sort,status,create_time');
    $data = $this->getAll($query);
   

    $file_name = '供需数据.xlsx';
    // 更新表头以匹配新的列数据
    $header = ['排序', '供应/需求', '产品分类', '关联企业', '是否推荐', '商品图片', '商品名称', '商品副标题', '商品详情','创建时间'];

    
    $filter = [
        // 'activity_status' => [
        //     ['value' => 0, 'label' => '进行中'],
        //     ['value' => 1, 'label' => '已结束'],
        //     ['value' => 2, 'label' => '未开始']
        // ]
    ];
    
    $data_attr = [];
    foreach($data as $value){
        $data_attr[] = [
            'sort_order' => $value['sort_order'],
            'supply_demand' => $value['supply_demand_text'],

            'product_category' => $value['product_category_text'],

            'enterprise_id' => $value['enterprise_id_text'],

            'is_recommend' => $value['is_recommend']==1?'是':'否',
            'image' => $value['image'],
            'product_name' => $value['product_name'],
            'product_subtitle' => $value['product_subtitle'],
            'supply_demand_datail' => $value['supply_demand_datail'],
            'create_time' => $value['create_time'],


        ];
    }

    $writer = new OpenSpoutWriter($file_name);
    // 更新列宽以匹配新的列数
    $writer->setWidth([20, 20, 20, 20, 20, 20, 20, 50, 20,20,20]);


    $writer->setHeader($header);
    $writer->setData($data_attr, null, $filter);
    $file_path = $writer->returnFile();
    return response()->download($file_path, urlencode($file_name));
}

}
